package com.hnucm.c201901020121.Fragment.My;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.hnucm.c201901020121.Activity.LoginAct;
import com.hnucm.c201901020121.Activity.MyTaskAct;
import com.hnucm.c201901020121.Activity.PublishManageAct;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BUser;
import com.hnucm.c201901020121.Utils.Interfaces.OnRecyclerItemClickListener;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

import static com.hnucm.c201901020121.Activity.LoginAct.currentUser;


public class MyFragment extends Fragment {

    ImageView toBeSubmit,audit,allTask,unqualified,finished,headimg,publishTask,refresh,tuichu;
    private OnRecyclerItemClickListener monItemClickListener;
    TextView nickName,amount,criditCore,sumIncome,userId;
    Boolean judge;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //Bmob.initialize(getContext(), "cedc891e5bb4f4c1");
        initView();
        getDate();
        judge=false;
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (judge == true) {
                    getDate();
                    judge=false;
                } else {
                    getDate1();
                    judge=true;
                }
            }
        });
        toBeSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), MyTaskAct.class);
                intent.putExtra("task","1");
                startActivity(intent);
            }
        });
        allTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), MyTaskAct.class);
                intent.putExtra("task","0");
                startActivity(intent);
            }
        });
        audit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), MyTaskAct.class);
                intent.putExtra("task","2");
                startActivity(intent);
            }
        });
        finished.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), MyTaskAct.class);
                intent.putExtra("task","3");
                startActivity(intent);
            }
        });
        unqualified.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), MyTaskAct.class);
                intent.putExtra("task","4");
                startActivity(intent);
            }
        });
        publishTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), PublishManageAct.class);
                startActivity(intent);
            }
        });
        tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), LoginAct.class);
                startActivity(intent);
            }
        });
    }

    private void getDate() {
        if(currentUser!=null){
            nickName.setText(currentUser.getUserName());
            amount.setText(currentUser.getAmount());
            criditCore.setText(String.valueOf(currentUser.getCriditCore()));
            sumIncome.setText(currentUser.getSumIncome());
            userId.setText(currentUser.getObjectId());
            Glide.with(MyFragment.this).load(currentUser.getHeadimg()).into(headimg);
            return;
        }
        BmobQuery<BUser> bmobQuery = new BmobQuery<BUser>();
        bmobQuery.getObject("WvYL111K", new QueryListener<BUser>() {
            @Override
            public void done(BUser object, BmobException e) {
                if(e==null){
                    if(currentUser==null) {
                        currentUser = object;
                    }
                    nickName.setText(object.getUserName());
                    amount.setText(object.getAmount());
                    criditCore.setText(String.valueOf(object.getCriditCore()));
                    sumIncome.setText(object.getSumIncome());
                    userId.setText(object.getObjectId());
                    Glide.with(MyFragment.this).load(object.getHeadimg()).into(headimg);
                }else{
                    Log.i("bmob",e.toString());
                    Toast.makeText(getContext(),"登录失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void getDate1() {
        BmobQuery<BUser> bmobQuery = new BmobQuery<BUser>();
        bmobQuery.getObject("2B3t888A", new QueryListener<BUser>() {
            @Override
            public void done(BUser object1, BmobException e) {
                if(e==null){

                    if(currentUser==null) {
                        currentUser = object1;
                    }
                    nickName.setText(object1.getUserName());
                    amount.setText(object1.getAmount());
                    criditCore.setText(String.valueOf(object1.getCriditCore()));
                    sumIncome.setText(object1.getSumIncome());
                    userId.setText(object1.getObjectId());
                    Glide.with(MyFragment.this).load(object1.getHeadimg()).into(headimg);
                }else{

                }
            }
        });
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YCAppBar.setStatusBarColor(getActivity(), ContextCompat.getColor(getContext(),R.color.tt));
    }
    private void initView(){
        toBeSubmit=getActivity().findViewById(R.id.imageView10);
        audit=getActivity().findViewById(R.id.imageView11);
        headimg=getActivity().findViewById(R.id.imageView2);
        nickName=getActivity().findViewById(R.id.textView27);
        amount=getActivity().findViewById(R.id.textView13);
        criditCore=getActivity().findViewById(R.id.textView14);
        sumIncome=getActivity().findViewById(R.id.textView15);
        allTask=getActivity().findViewById(R.id.imageView19);
        unqualified=getActivity().findViewById(R.id.imageView13);
        finished=getActivity().findViewById(R.id.imageView12);
        userId=getActivity().findViewById(R.id.textView89);
        publishTask=getActivity().findViewById(R.id.imageView15);
        refresh=getActivity().findViewById(R.id.imageView65);
        tuichu=getActivity().findViewById(R.id.imageView23);
    }
}